import 'dart:async';

import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lcapp/api/interface/zixun_api.dart';
import 'package:lcapp/api/request/response_data.dart';
import 'package:lcapp/basic/local_info.dart';
import 'package:lcapp/basic/user_info.dart';
import 'package:lcapp/config/const_config.dart';
import 'package:lcapp/model/event/event_bus.dart';
import 'package:lcapp/page/home/home_zx_list_page.dart';
import 'package:lcapp/widget/custom_indicator.dart';
import 'package:lcapp/widget/lc_gesture_detector.dart';

class HomeZixunPage extends StatefulWidget {
  const HomeZixunPage({Key? key}) : super(key: key);

  @override
  _HomeZixunPageState createState() => _HomeZixunPageState();
}

class _HomeZixunPageState extends State<HomeZixunPage> with TickerProviderStateMixin,AutomaticKeepAliveClientMixin{

  TabController? tabController;
  List zixuns = [];
  StreamSubscription? _subscription;
  StreamSubscription? _wifiSubscription;
  bool isDone = false;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _subscription = eventBus.on<PayState>().listen((event) {
      if (event.state == 0) {
        setState(() {});
      }
    });
    _getZixunTtpes();
  }

  Future<void> _getZixunTtpes()async{
    if(isDone){
      return;
    }
    ResponseData res = await ZiXunAPI.getZiXunTypes();
    zixuns.clear();
    if(res.code==200){
      isDone = true;
      zixuns = res.data??[];
    }
    zixuns.insert(0,{
      'id':0,
      'name':'综合',
      'vipFlag':0,
    });
    zixuns.insert(1,{
      'id':0,
      'name':'VIP资讯',
      'vipFlag':1,
    });
    tabController = TabController(length: zixuns.length, vsync: this);
    if(mounted){
      setState(() {});
    }
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _subscription?.cancel();
    _subscription=null;
    _wifiSubscription?.cancel();
    _wifiSubscription = null;
  }

  @override
  Widget build(BuildContext context) {
    return isDone? NestedScrollView(
      headerSliverBuilder: (context,vale){
        return [
          SliverToBoxAdapter(
            child: Container(
              margin: EdgeInsets.only(bottom: 20.w,top: 20.w),
              padding: EdgeInsets.symmetric(horizontal: 30.w),

              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: [
                  lcGestureDetector(
                    isLogin: true,
                    onTap: (){
                      // if(userInfo.vip!=1){
                      //   SmartDialog.show(
                      //     alignmentTemp: Alignment.center,
                      //     clickBgDismissTemp: true,
                      //     maskColorTemp: Color.fromRGBO(0, 0, 0, 0.5),
                      //     widget: Container(
                      //       child: Column(
                      //         mainAxisAlignment: MainAxisAlignment.end,
                      //         children: [
                      //           Expanded(child: Container()),
                      //           Container(
                      //             child: Column(
                      //               children: [
                      //                 Container(
                      //                   child: Text('您还不是会员',style: TextStyle(
                      //                       color: Colors.black,
                      //                       fontSize: 35.sp,
                      //                       fontWeight: FontWeight.w700
                      //                   ),),
                      //                   margin: EdgeInsets.only(bottom: 20.w),
                      //                 ),
                      //                 Container(
                      //                   child: Text('请开通后查看',style: TextStyle(
                      //                       color: Color(0xff242424),
                      //                       fontSize: 30.sp,
                      //                       fontWeight: FontWeight.w500
                      //                   ),),
                      //                 ),
                      //                 Container(
                      //                   margin: EdgeInsets.symmetric(vertical: 40.w,horizontal: 0.w),
                      //                   height: 1,
                      //                   color: Colors.grey,
                      //                 ),
                      //                 GestureDetector(
                      //                   onTap: (){
                      //                     SmartDialog.dismiss();
                      //                     Navigator.of(context).pushNamed("MinVipPage").then((value){
                      //                       refreshUserInfo();
                      //                     });
                      //                   },
                      //                   child: Container(
                      //                     // padding: EdgeInsets.symmetric(horizontal: 60.w),
                      //                     child: Row(
                      //                       children: [
                      //                         Expanded(child: Text('开通会员',textAlign: TextAlign.left,style: TextStyle(
                      //                             color: Color(0xff232323),
                      //                             fontSize: 30.sp,
                      //                             fontWeight: FontWeight.w600
                      //                         ),)),
                      //                         Icon(Icons.arrow_forward_ios,size: 30.sp,)
                      //                       ],
                      //                     ),
                      //                   ),
                      //                 ),
                      //                 GestureDetector(
                      //                   onTap: (){
                      //                     SmartDialog.dismiss();
                      //                   },
                      //                   child: Container(
                      //                     width: 1.sw-120.w,
                      //                     height: 90.w,
                      //                     alignment: Alignment.center,
                      //                     margin: EdgeInsets.only(top: 50.w),
                      //                     decoration: BoxDecoration(
                      //                         color: Color(0xffEBEBEB),
                      //                         borderRadius: BorderRadius.circular(10.w)
                      //                     ),
                      //                     child: Text('取消',style: TextStyle(
                      //                         color: Color(0xff232323),
                      //                         fontSize: 30.sp,
                      //                         fontWeight: FontWeight.w500
                      //                     ),),
                      //                   ),
                      //                 )
                      //               ],
                      //             ),
                      //             decoration: BoxDecoration(
                      //                 color: Colors.white,
                      //                 borderRadius: BorderRadius.only(topLeft: Radius.circular(15.w),topRight: Radius.circular(15.w))
                      //             ),
                      //             padding: EdgeInsets.only(left: 60.w,right: 60.w,top: 30.w,bottom: 20.w+ScreenUtil().bottomBarHeight),
                      //           )
                      //         ],
                      //       ),
                      //     ),
                      //   );
                      //   return;
                      // }
                      Navigator.of(context).pushNamed('FaxianWebviewPage',arguments: {
                        'url':'${BasVueurl}/#/shopPrice?token=${AsrLocalInfo.token}&isvip=${userInfo.vip}',
                        'title':'厂家报价'
                      });
                    },
                    child: Container(
                      height: 70.w,
                      width: 200.w,
                      decoration: BoxDecoration(
                          color: Color(0x5fFFB48E),
                          borderRadius: BorderRadius.circular(10.w)
                      ),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Image.asset('assets/home/cjbj_w.png',width: 30.w,),
                          SizedBox(width: 20.w,),
                          Text('厂家报价',style: TextStyle(
                              color: Colors.white,
                              fontSize: 28.sp
                          ),),
                        ],
                      ),
                    ),
                  ),
                  lcGestureDetector(
                    isLogin: true,
                    onTap: (){
                      Navigator.of(context).pushNamed('FaxianWebviewPage',arguments: {
                        'url':'${BasVueurl}/#/jinKou?token=${AsrLocalInfo.token}&isvip=${userInfo.vip}',
                        'title':'进口统计'
                      });
                    },
                    child: Container(
                      height: 70.w,
                      width: 200.w,
                      decoration: BoxDecoration(
                          color: Color(0x5fFFB48E),
                          borderRadius: BorderRadius.circular(10.w)
                      ),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Image.asset('assets/home/jktj_w.png',width: 30.w,),
                          SizedBox(width: 20.w,),
                          Text('进口统计',style: TextStyle(
                              color: Colors.white,
                              fontSize: 28.sp
                          ),),
                        ],
                      ),
                    ),
                  ),
                  lcGestureDetector(
                    isLogin: true,
                    onTap: (){
                      // if(userInfo.vip!=1){
                      //   SmartDialog.show(
                      //     alignmentTemp: Alignment.center,
                      //     clickBgDismissTemp: true,
                      //     maskColorTemp: Color.fromRGBO(0, 0, 0, 0.5),
                      //     widget: Container(
                      //       child: Column(
                      //         mainAxisAlignment: MainAxisAlignment.end,
                      //         children: [
                      //           Expanded(child: Container()),
                      //           Container(
                      //             child: Column(
                      //               children: [
                      //                 Container(
                      //                   child: Text('您还不是会员',style: TextStyle(
                      //                       color: Colors.black,
                      //                       fontSize: 35.sp,
                      //                       fontWeight: FontWeight.w700
                      //                   ),),
                      //                   margin: EdgeInsets.only(bottom: 20.w),
                      //                 ),
                      //                 Container(
                      //                   child: Text('请开通后查看',style: TextStyle(
                      //                       color: Color(0xff242424),
                      //                       fontSize: 30.sp,
                      //                       fontWeight: FontWeight.w500
                      //                   ),),
                      //                 ),
                      //                 Container(
                      //                   margin: EdgeInsets.symmetric(vertical: 40.w,horizontal: 0.w),
                      //                   height: 1,
                      //                   color: Colors.grey,
                      //                 ),
                      //                 GestureDetector(
                      //                   onTap: (){
                      //                     SmartDialog.dismiss();
                      //                     Navigator.of(context).pushNamed("MinVipPage").then((value){
                      //                       refreshUserInfo();
                      //                     });
                      //                   },
                      //                   child: Container(
                      //                     // padding: EdgeInsets.symmetric(horizontal: 60.w),
                      //                     child: Row(
                      //                       children: [
                      //                         Expanded(child: Text('开通会员',textAlign: TextAlign.left,style: TextStyle(
                      //                             color: Color(0xff232323),
                      //                             fontSize: 30.sp,
                      //                             fontWeight: FontWeight.w600
                      //                         ),)),
                      //                         Icon(Icons.arrow_forward_ios,size: 30.sp,)
                      //                       ],
                      //                     ),
                      //                   ),
                      //                 ),
                      //                 GestureDetector(
                      //                   onTap: (){
                      //                     SmartDialog.dismiss();
                      //                   },
                      //                   child: Container(
                      //                     width: 1.sw-120.w,
                      //                     height: 90.w,
                      //                     alignment: Alignment.center,
                      //                     margin: EdgeInsets.only(top: 50.w),
                      //                     decoration: BoxDecoration(
                      //                         color: Color(0xffEBEBEB),
                      //                         borderRadius: BorderRadius.circular(10.w)
                      //                     ),
                      //                     child: Text('取消',style: TextStyle(
                      //                         color: Color(0xff232323),
                      //                         fontSize: 30.sp,
                      //                         fontWeight: FontWeight.w500
                      //                     ),),
                      //                   ),
                      //                 )
                      //               ],
                      //             ),
                      //             decoration: BoxDecoration(
                      //                 color: Colors.white,
                      //                 borderRadius: BorderRadius.only(topLeft: Radius.circular(15.w),topRight: Radius.circular(15.w))
                      //             ),
                      //             padding: EdgeInsets.only(left: 60.w,right: 60.w,top: 30.w,bottom: 20.w+ScreenUtil().bottomBarHeight),
                      //           )
                      //         ],
                      //       ),
                      //     ),
                      //   );
                      //   return;
                      // }

                      Navigator.of(context).pushNamed('FaxianWebviewPage',arguments: {
                        'url':'${BasVueurl}/#/carInfo?token=${AsrLocalInfo.token}&isvip=${userInfo.vip}',
                        'title':'车辆情况'
                      });
                    },
                    child: Container(
                      height: 70.w,
                      width: 200.w,
                      decoration: BoxDecoration(
                          color: Color(0x5fFFB48E),
                          borderRadius: BorderRadius.circular(10.w)
                      ),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Image.asset('assets/home/clqk_w.png',width: 30.w,),
                          SizedBox(width: 20.w,),
                          Text('车辆情况',style: TextStyle(
                              color: Colors.white,
                              fontSize: 28.sp
                          ),),
                        ],
                      ),
                    ),
                  )
                ],
              ),
            ),
          ),
          SliverAppBar(
            leading: Container(),
            leadingWidth: 0,
            toolbarHeight: 0,
            backgroundColor: defaultBarColor,
            pinned:true,
            primary: false,
            floating: true,
            bottom: PreferredSize(
              child: Container(
                width: 1.sw,
                padding: EdgeInsets.symmetric(horizontal: 5.w),
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.only(topLeft: Radius.circular(35.w),topRight: Radius.circular(35.w))
                ),
                alignment: Alignment.center,
                height: 110.w,
                child: TabBar(
                  controller: tabController,
                  isScrollable:true,
                  // indicator: CustomIndicator(
                  //     type: CustomIndicatorType.LineRect,
                  //     height: 1,
                  //     width: 20.w,
                  //     color: defaultBarColor),
                  // indicatorPadding: EdgeInsets.only(top: -20.w),
                  labelColor: defaultBarColor,
                  labelStyle: TextStyle(
                    fontSize: 30.sp,
                    color: defaultBarColor,
                    fontWeight: FontWeight.w600,
                  ),
                  unselectedLabelColor: Colors.black,
                  unselectedLabelStyle: TextStyle(
                    fontSize: 30.sp,
                    color: Colors.black,
                    fontWeight: FontWeight.w400,
                  ),
                  tabs: zixuns.map((item) {
                    String name = item['name'];
                    if(item['vipFlag'] == 1){
                      return Container(
                        child: Image.asset('assets/home/vip_zx_tab.png',height: 30.w,),
                      );
                    }
                    return Tab(text: item['name']);
                  }).toList(),
                ),
              ),
              preferredSize: Size(1.sw,110.w),
            ),
            elevation: 0,
          ),
        ];
      },
      body: TabBarView(
        controller: tabController,
        children: zixuns.map((item) {
          return HomeZXListPage(typeId: item['id'],vipFlag: item['vipFlag']??0,);
        }).toList(),
      ),
    ):Container();
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
